package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillExamineBiz;
import com.egao.orderB.entity.AddressFactory;
import com.egao.orderB.entity.Bill;
import com.egao.orderB.entity.BillExamine;
import com.egao.orderB.service.BillExamineService;
import net.sf.json.JSONArray;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.egao.orderB.mapper.BillExamineMapper;
//import javax.transaction.Transactional;

/**
 * 运单审核记录服务实现类
 * @author : Doman
 * @date : 2021-04-13 10:40:47
 * @version ：1.0
 */
@Service
public class BillExamineServiceImpl /*extends ServiceImpl<BillExamineMapper, BillExamine> */implements BillExamineService {

	@Autowired
	private BillExamineBiz billExamineBiz;

    /**
    @Override
    public PageResult<BillExamine> listPage(PageParam<BillExamine> page) {
        List<BillExamine> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<BillExamine> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, BillExamine billExamine, User loginUser)  throws Exception{
        JsonResult jr = billExamineBiz.list(page,billExamine,loginUser);
        List<BillExamine> list = (List<BillExamine>)jr.getData();
        if(list!=null && list.size()>0) {
            for(BillExamine b: list) {
                JSONArray jsonArray = JSONArray.fromObject(b.getFactoryAddressJson());
                List<AddressFactory> afs = (List)JSONArray.toCollection(jsonArray,AddressFactory.class);
                b.setAddressFactoryList(afs);
            }
        }
        return jr;
    }

    @Override
    public JsonResult list(BillExamine billExamine, User loginUser) {
        List<BillExamine> list =billExamineBiz.list(billExamine,loginUser);
        if(list!=null && list.size()>0) {
            for(BillExamine b: list) {
                JSONArray jsonArray = JSONArray.fromObject(b.getFactoryAddressJson());
                List<AddressFactory> afs = (List)JSONArray.toCollection(jsonArray,AddressFactory.class);
                b.setAddressFactoryList(afs);
            }
        }
        return JsonResult.ok().setData(list);
    }

    @Override
    public JsonResult getById(BillExamine billExamine) {
        BillExamine b = billExamineBiz.getById(billExamine);
        JSONArray jsonArray = JSONArray.fromObject(b.getFactoryAddressJson());
        List<AddressFactory> afs = (List)JSONArray.toCollection(jsonArray,AddressFactory.class);
        b.setAddressFactoryList(afs);
        return JsonResult.ok().setData(b);
    }

    @Transactional
    @Override
    public JsonResult save(BillExamine billExamine, User loginUser) {
        if (billExamineBiz.save(billExamine, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult save(Bill billExamine, User loginUser) {
        if (billExamineBiz.save(billExamine, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(BillExamine billExamine, User loginUser) {
        if (billExamineBiz.update(billExamine, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(BillExamine billExamine, User loginUser) {
        if (billExamineBiz.remove(billExamine, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
